package com.zpa.redisstreamlistener.controller;

import com.zpa.redisstreamlistener.processor.RedisStreamProducer;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/streamProducer")
public class RedisStreamController {

    /**
     * 单机模式
     */
    @Autowired
    private RedissonClient redisson1;
    /**
     * 集群模式
     */
    @Autowired
    @Qualifier("redisson2")
    private RedissonClient redisson2;

    /**
     * 发送消息
     */
    @PostMapping("/send/json")
    public String send(@RequestBody User user) {
        RedissonClient redisson = redisson1;
        if (user.factory == 2){
            redisson = redisson2;
        }
        RedisStreamProducer redisStreamProducer = new RedisStreamProducer(redisson);
        redisStreamProducer.publishMessage(user.streamName, user);
        return "success";
    }


    /**
     * 测试数据
     */
    @Data
    public static class User {
        // 一些测试数据
        private String name;
        private Integer age;
        private String sex;

        // stream 流的名称 就是topic
        private String streamName;

        // 工厂
        // 1 redisson1 单机
        // 2 redisson2 集群
        private int factory = 1;
    }
}
